#!/usr/bin/python
# -*- #coding:utf8

__author__ = '$Author: bo.feng $'
__version__ = '$Revision: 1.32 $'
__date__ = '$Date: 2011/03/25 10:54:03 $'


import MySQLdb
import datetime

def get_rs(conn, sql):
    """
    get record set by sql statement(select).
    """
    try:
        cursor = conn.cursor()
        cursor.execute(sql)
        rows = cursor.fetchall()
        conn.commit()
        return rows
    except:
        pass
    finally:
        try:
            cursor.close()
        except:
            pass

def exec_sql(conn, sql):
    """
    execute sql statement(insert,update,delete)
    """
    try:
        print sql
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
    except:
        pass;
    finally:
        try:
            cursor.close()
        except:
            pass


today = datetime.date.today()
dt1 = datetime.datetime.strftime(today,'%Y-%m-%d')
conn = MySQLdb.connect(host='localhost',user='root',passwd='shzygjrmdwg', db='cprt')

rows = get_rs(conn,"select u2.user_id from USER_DETAIL u2 inner join auth_user u on u2.user_id = u.id where u.is_active = 1" )
for row in rows:
    exec_sql(conn,"insert into CHECKIN_LOG(user_id,created_date) select %d, '%s' from dual where not exists(select * from CHECKIN_LOG where user_id = %d and datediff(created_date, '%s') = 0)" % (row[0],dt1,row[0],dt1))
conn.close()





